using System.Net;
using Xunit;

namespace CodePlex.TfsLibrary.ClientEngine
{
    public class TfsEngineCredentialTest
    {
        [Fact]
        public void CallingGetCredentialsMultipleTimesOnlyCallsCallbackOnceWithSameUrl()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();
            ICredentials callbackCreds = new NetworkCredential("Jim", "Bob");
            string tfsUrl = "http://tfsUrl";
            int callbackCount = 0;
            engine.CredentialsCallback = delegate
                                         {
                                             callbackCount++;
                                             return callbackCreds;
                                         };

            engine.GetCredentials(tfsUrl);
            engine.GetCredentials(tfsUrl);

            Assert.Equal(1, callbackCount);
        }

        [Fact]
        public void CallingGetCredentialsWithDifferentUrlsCachesCorrectly()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();
            ICredentials callbackCreds = new NetworkCredential("Jim", "Bob");
            string tfsUrl1 = "http://tfsUrl1";
            string tfsUrl2 = "http://tfsUrl2";
            int callbackCount = 0;
            engine.CredentialsCallback = delegate
                                         {
                                             callbackCount++;
                                             return callbackCreds;
                                         };

            engine.GetCredentials(tfsUrl1);
            engine.GetCredentials(tfsUrl2);

            Assert.Equal(2, callbackCount);
        }

        [Fact]
        public void CanForceGettingNewCredentials()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();
            ICredentials callbackCreds = new NetworkCredential("Jim", "Bob");
            string tfsUrl = "http://tfsUrl";
            int callbackCount = 0;
            engine.CredentialsCallback = delegate
                                         {
                                             callbackCount++;
                                             return callbackCreds;
                                         };

            engine.GetCredentials(tfsUrl);
            engine.GetCredentials(tfsUrl, true);

            Assert.Equal(2, callbackCount);
        }

        [Fact]
        public void CredentialCallbackCanBeNull()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();

            ICredentials creds = engine.GetCredentials("http://tfsUrl1");

            Assert.Null(creds);
        }

        [Fact]
        public void WillCallbackForCredentialRequest()
        {
            TestableTfsEngine engine = TestableTfsEngine.Create();
            ICredentials callbackCreds = new NetworkCredential("Jim", "Bob");
            string callbackUrl = "";
            engine.CredentialsCallback = delegate(ICredentials credentials,
                                                  string url)
                                         {
                                             callbackUrl = url;
                                             return callbackCreds;
                                         };

            ICredentials creds = engine.GetCredentials("http://tfsUrl");

            Assert.Same(callbackCreds, creds);
            Assert.Equal("http://tfsUrl", callbackUrl);
        }
    }
}